// pages/editPwd/pwd.js
var app = getApp()
Page({
  data:{
    oldPwd:'',
    newPwd:'',
    confirmPwd:'',
    loading:false
  },
  onLoad:function(options){
    // 页面初始化 options为页面跳转所带来的参数
  },
  onReady:function(){
    // 页面渲染完成
  },
  onShow:function(){
    // 页面显示
  },
  onHide:function(){
    // 页面隐藏
  },
  onUnload:function(){
    // 页面关闭
  },
  formSubmit: function(e){
    var that = this;
    that.setData({
      loading: true
    })
    // 校验原密码是否为空
    if(e.detail.value.oldPwd.length < 1){
      wx.showModal({
        // title: '提示',
        showCancel:false,
        content: '请输入原始密码！',
        success: function(res) {
          that.setData({
            loading: false
          });
          if (res.confirm) {
            console.log('用户点击确定');
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      });
    return;
    }
    // 校验新密码是否为空
    if(e.detail.value.newPwd.length < 1){
      wx.showModal({
        // title: '提示',
        showCancel:false,
        content: '请输入新密码！',
        success: function(res) {
          that.setData({
            loading: false
          });
          if (res.confirm) {
            console.log('用户点击确定');
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      });
    return;
    }
    // 校验新密码格式
    if(e.detail.value.newPwd.length > 0){
      var regExp = /^[a-zA-Z]\w{5,17}$/;
      if(!regExp.test(e.detail.value.newPwd)){
        wx.showModal({
          // title: '提示',
          showCancel:false,
          content: '密码以字母开头，长度在6~18之间，只能包含字母、数字和下划线',
          success: function(res) {
            that.setData({
              loading: false
            });
            if (res.confirm) {
              console.log('用户点击确定');
            } else if (res.cancel) {
              console.log('用户点击取消')
            }
          }
        });
        return;
      }
    }
    // 校验确认密码是否为空
    if(e.detail.value.confirmPwd.length < 1){
      wx.showModal({
        // title: '提示',
        showCancel:false,
        content: '请输入确认密码！',
        success: function(res) {
          that.setData({
            loading: false
          });
          if (res.confirm) {
            console.log('用户点击确定');
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      });
    return;
    }
    // 校验确认密码格式
    if(e.detail.value.confirmPwd.length > 0){
      var regExp = /^[a-zA-Z]\w{5,17}$/;
      if(!regExp.test(e.detail.value.confirmPwd)){
        wx.showModal({
          // title: '提示',
          showCancel:false,
          content: '密码以字母开头，长度在6~18之间，只能包含字母、数字和下划线',
          success: function(res) {
            that.setData({
              loading: false
            });
            if (res.confirm) {
              console.log('用户点击确定');
            } else if (res.cancel) {
              console.log('用户点击取消')
            }
          }
        });
        return;
      }
    }
    // 校验两次密码一致性
    if(e.detail.value.confirmPwd.length > 0 && e.detail.value.newPwd.length > 0){
      // var regExp = /^[a-zA-Z]\w{5,17}$/;
      if(e.detail.value.confirmPwd != e.detail.value.newPwd){
        wx.showModal({
          // title: '提示',
          showCancel:false,
          content: '两次密码不一致！',
          success: function(res) {
            that.setData({
              loading: false
            });
            if (res.confirm) {
              console.log('用户点击确定');
            } else if (res.cancel) {
              console.log('用户点击取消')
            }
          }
        });
        return;
      }
    }
    wx.request({
      url: app.globalData.API + '/serverProvider/updatePassword',
      method:'POST',
      header: {
        "Cookie":wx.getStorageSync('sessionid')
      },
      data: {
        id:wx.getStorageSync('loginInfo').user.id,
        oldPassword: e.detail.value.oldPwd,
	      password: e.detail.value.newPwd,
      },
      success: function(res) {
        console.log(res);
        if(res.data.code == 10000){
          wx.showToast({
            title: '修改成功，请重新登录！',
            icon: 'success',
            duration: 2000
          })
          // wx.setStorageSync('loginInfo', res.data);
          setTimeout(function(){
            wx.navigateTo({
            url: '../index/index'
          })
          },2500);
        }else{
          wx.showModal({
            title: res.data.msg,
            showCancel:false,
          });
        }
      },
      fail: function(){
        console.log('接口调用失败');
      },
      complete: function(){
        that.setData({
            loading: false
          });
      }
    })
    console.log('form发生了submit事件，携带数据为：',e.detail.value);
  }
})